package com.junpuxin.mapper;

import com.junpuxin.dto.EmpPageDTO;
import com.junpuxin.entity.Emp;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author junpuxin
 */
@Repository
public interface EmpMapper {

    @Insert("""
            insert into ums_emp (
            username, password, avatar, phone, wechat, email, gender, age, province,
            address, realname, idcard, fk_dept_id, info, hiredate, version, deleted, created, updated
            )
            values (
            #{username}, #{password}, #{avatar}, #{phone}, #{wechat}, #{email}, 
            #{gender}, #{age}, #{province}, #{address}, #{realname}, #{idcard}, #{fkDeptId}, #{info}, 
            #{hiredate}, #{version}, #{deleted}, #{created}, #{updated}
            )
            """)
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Emp emp);


    @Results(
            id = "empResultMap",
            value = {
                    @Result(property = "id", column = "id", id = true),
                    @Result(property = "fkDeptId", column = "fk_dept_id"),
                    @Result(property = "dept", column = "fk_dept_id", one = @One(select = "com.wanghaoran.mapper.DeptMapper.select")),
            }
    )
    @Select("""
            select * from ums_emp t
            where t.id = #{param1} and t.deleted = 0
            """)
    Emp select(Long id);

    @ResultMap("empResultMap")
    @Select("""
            <script>
            select * from ums_emp t
            <where>
                <if test='username != null'> username = #{username} and </if>
                <if test='phone != null'> phone = #{phone} and </if>
                <if test='realname != null'> realname = #{realname} and </if>
                <if test='idcard != null'> idcard = #{idcard} and </if>
                <if test='fkDeptId != null'> fk_dept_id = #{fkDeptId} and </if>
                t.deleted = 0
            </where>
            </script>
            """)
    List<Emp> list(EmpPageDTO dto);

    @Update("""
            <script>
            update ums_emp
            <set>
               <if test = 'username != null'> username = #{username}, </if>
               <if test = 'password != null'> password = #{password}, </if>
               <if test = 'avatar != null'> avatar = #{avatar}, </if>
               <if test = 'phone != null'> phone = #{phone}, </if>
               <if test = 'wechat != null'> wechat = #{wechat}, </if>
               <if test = 'email != null'> email = #{email}, </if>
               <if test = 'gender != null'> gender = #{gender}, </if>
               <if test = 'age != null'> age = #{age}, </if>
               <if test = 'province != null'> province = #{province}, </if>
               <if test = 'address != null'> address = #{address}, </if>
               <if test = 'realname != null'> realname = #{realname}, </if>
               <if test = 'idcard != null'> idcard = #{idcard}, </if>
               <if test = 'fkDeptId != null'> fk_dept_id = #{fkDeptId},</if>
               <if test = 'info != null'> info = #{info},</if>
               <if test = 'hiredate != null'> hiredate = #{hiredate},</if>
               <if test = 'version != null'>version = #{version},</if>
               <if test = 'deleted != null'>deleted = #{deleted},</if>
               <if test = 'created != null'>created = #{created},</if>
               <if test = 'updated != null'>updated = #{updated},</if>
               version = version + 1
            </set>
            where id = #{id} and deleted = 0 and version = #{version}
            </script>
            """)
    int update(Emp emp);


    @Update("""
            update ums_emp set deleted = 1, updated = current_timestamp where id = #{id}
            """)
    int delete(Long id);

    @Update("""
            <script>
            update ums_emp set deleted = 1, updated = current_timestamp where 
            <foreach collection="list" item="e" open="id in (" close=")" separator=",">
                ${e}
            </foreach>
            </script>
            """)
    int deleteBatch(List<Long> ids);

    @Update("""
            update ums_emp set password = #{param1}, updated = now()
            where password = #{param2} and id = #{param3} and deleted = 0
            """)
    int updatePassword(String newPassword, String oldPassword, Long id);

    @Select("""
            select * from ums_emp tablesample 
            where t.id = #{param2} and t.password = #{param1} and t.deleted = 0
            """)
    Emp selectPassword(String password, Long id);


    @ResultMap("empResultMap")
    @Select("""
            select * from ums_emp t
            where t.username = #{param1} and t.password = #{param2} and t.deleted = 0
            """)
    Emp selectByAccount(String username, String password);

}
